//*************************************************************************
// File Name: Menus.cpp
// This is the implementation file for the functions
// used to navigate the menu
// "Group 3"
// CS 1C
// March 25, 2010
// Group Project: SuperWarehouse
// Due: March 25, 2010
// - KZ's comment
//*************************************************************************

/****************************************************************
 * CHECKLIST:
 * 		MAIN MENU
 * 		MEMBERS MENU
 * 			INITIALIZE CUSTOMER LIST
 * 			ADD MEMBER
 * 			REPORTS MENU
 * 			RETURN TO MAIN
 * 		SALES MENU
 * 			PROCESS SALES
 * 			REPORTS MENU
 * 			RETURN TO MAIN
 * 		REPORTS MENU
 * 			SALES REPORTS
 * 			MEMBER REPORTS
 * 			RETURN TO MAIN
 * 				SALES REPORTS
 * 					DAILY SALES REPORT
 * 					RETURN TO REPORTS MENU
 * 				MEMBER REPORTS
 * 					EXPIRATION REPORT
 * 					REBATE REPORTS
 *
 ******************************************************************/
#include "Menus.h"
#include "MemberList.h"
#include "MemberManager.h"
#include "ReportingService.h"
#include "CheckoutService.h"
#include "functions.h"

void MainMenu()
{
	int choice;
	enum MenuOptions{SALES = 1, MEMBERS, REPORTS, EXIT};
	PrintSpacer();
	DisplayMainMenu();
	choice = MenuInput(4);
	switch(choice)
	{
	case SALES:
		SalesMenu();
		break;
	case MEMBERS:
		MembersMenu();
		break;
	case REPORTS:
		ReportsMenu();
		break;
	case EXIT:
		break;
	}
}

void ReportsMenu()
{
	int choice;
	enum MenuOptions{SALES = 1, MEMBERS, RE_MAIN};
	PrintSpacer();
	DisplayReportsMenu();
	choice = MenuInput(3);
	switch(choice)
	{
	case SALES:
		SalesReportMenu();
		break;
	case MEMBERS:
		MembersReportMenu();
		break;
	case RE_MAIN:
	default:
		MainMenu();
		break;
	}
}
/*************************************************
 * STILL NEED APPROPRAITE FUNCTIONS TO CALL FOR
 * THIS MENU SUBSYSTEM
 *************************************************/
void SalesReportMenu()
{
	int choice;
	enum MenuOptions{DAILY = 1, RE_MENU};
	ReportingService *reportingService;
	string reports, reportName;


	PrintSpacer();
	cout << setw(10) << " "
			<< "*****************************************************\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "*                  SALES REPORTS                    *\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "*              1 - Daily Sales                      *\n";
	cout << setw(10) << " "
			<< "*              2 - Return to Reports                *\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "*****************************************************\n";
	cout << setw(10) << " " << "PLEASE ENTER A SELECTION: ";
	choice = MenuInput(3);
	cin.ignore(100, '\n');


	switch(choice){
	case DAILY:
		reportingService = new ReportingService();
		reports = reportingService->listAvailableSalesReports();
		if (reports.length()) {
			cout << "Available reports:" << endl << reports << endl;
			cout << "Enter report name: ";
			cin >> reportName;
			cin.ignore(100, '\n');
			PrintSpacer();
			reportingService->printSalesReport(reportName);
		} else {
		}
		delete reportingService;
		break;
	case RE_MENU:
		ReportsMenu();
		return;
		break;
	}

	Pause();
	MainMenu();
}
/*************************************************
 * STILL NEED APPROPRAITE FUNCTIONS TO CALL FOR
 * THIS MENU SUBSYSTEM
 *************************************************/
void MembersReportMenu()
{
	int choice;
	enum MenuOptions{EXPIRE = 1, SUPER, BASIC, ALL, INDIV, RE_MENU};
	MemberManager *manager;
	Member *m;
	MemberList *l;
	ReportingService *reportingService;
	string dateStr;
	int memberId;
	time_t start, end;

	manager = MemberManager::instance();
	reportingService = new ReportingService();

	PrintSpacer();
	cout << setw(10) << " "
			<< "*****************************************************\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "*                 MEMBER REPORTS                    *\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "* 1 - Expiring Members     2 - Super members        *\n";
	cout << setw(10) << " "
			<< "* 3 - Basic Members	     4 - All members          *\n";
	cout << setw(10) << " "
			<< "* 5 - Individual Members   6 - Return to Reports    *\n";
	cout << setw(10) << " "
			<< "*                                                   *\n";
	cout << setw(10) << " "
			<< "*****************************************************\n";
	cout << setw(10) << " " << "PLEASE ENTER A SELECTION: ";
	choice = MenuInput(6);
	cin.ignore(100, '\n');

	switch(choice)
	{
	case EXPIRE:
		//Expiring member report function call
		cout << "Retrieve members expiring within 30 days of date"
				<< " (MM/DD/YYYY): ";
		getline(cin, dateStr);
		start = timestampFromDateString(dateStr);
		end = nextMonth(start);
		PrintSpacer();
		reportingService->printExpiringMemberList(
				manager->getExpiringMembers(start, end));
		break;
	case SUPER:
		reportingService->printMemberList(manager->getSuperMembers());
		break;
	case BASIC:
		reportingService->printMemberList(manager->getBasicMembers());
		break;
	case ALL:
		reportingService->printMemberList(manager->getAllMembers());
		break;
	case INDIV:
		cout << "Print information for member ID: ";
		cin >> memberId;
		m = manager->getAllMembers()->findMemberById(memberId);
		if (m != NULL) {
			l = new MemberList();
			l->addMember(m);
			reportingService->printMemberList(l);
			delete l;
		} else {
			errorMsg("Unable to find member");
		}
		break;
	case RE_MENU:
		ReportsMenu();
		return;
		break;
	}

	Pause();
	MainMenu();
}

void SalesMenu()
{
	int choice;
	enum MenuOptions{PROCESS = 1,REPORTS, RE_MAIN};
	string fileName;
	MemberManager *manager = MemberManager::instance();
	CheckoutService *checkout;

	PrintSpacer();
	DisplaySalesMenu();
	choice = MenuInput(3);
	cin.ignore(100, '\n');

	switch(choice)
	{
	case PROCESS:
		checkout = new CheckoutService(manager);
		cout << "Please specify file to import from: ["
				<< DEFAULT_SALES_IMPORT_FILE << "] ";
		getline(cin, fileName);
		chomp(fileName);
		if (!fileName.length())
			fileName = string(DEFAULT_SALES_IMPORT_FILE);
		PrintSpacer();
		if (!checkout->processDailySalesFromFile(fileName)) {
			cout << "Error processing daily sales from " << fileName << endl;
		}
		delete checkout;
		break;
	case REPORTS:
		ReportsMenu();
		return;
		break;
	case RE_MAIN:
		MainMenu();
		return;
		break;
	default:
		cerr << "Invalid Selection.";
		break;
	}

	Pause();
	MainMenu();
}

void MembersMenu()
{
	int choice;
	string fileName;
	enum MenuOptions{INIT = 1, REPORTS, RE_MAIN};
	MemberManager *manager = MemberManager::instance();

	PrintSpacer();
	DisplayMembersMenu();

	choice = MenuInput(4);
	cin.ignore(100, '\n');

	switch(choice)
	{
	case INIT:
		cout << "Please specify file to import from: ["
				<< DEFAULT_CUSTOMER_IMPORT_FILE << "] ";
		getline(cin, fileName);
		chomp(fileName);
		if (!fileName.length()) {
			fileName = string(DEFAULT_CUSTOMER_IMPORT_FILE);
		}
		PrintSpacer();
		if (!manager->importMembersFromTextFile(fileName)) {
			cout << "Error importing members from " << fileName << endl;
		} else {
			cout << "Imported "
					<< manager->getAllMembers()->getNumMembers()
					<< " members." << endl;
		}
		break;
	case REPORTS:
		ReportsMenu();
		return;
		break;
	case RE_MAIN:
		MainMenu();
		return;
		break;
	default:
		cerr << "Invalid Selection.";
		break;
	}

	Pause();
	MainMenu();
}

void Scroll(string message, int size)
{
	int tab;

	tab = (80 - size)/2;
	(message.c_str());
	for(int P = 0; P < 20; P++)
	{
		cout << endl;
	}
	cout << setw(tab) << " ";
	for(int j = 0; j < size; j++)
	{
		cout << message[j];
		Timer();
	}
	for(int i = 0; i < 100; i++){Timer();}
}
void Timer()
{
	for(int z = 0; z < 10000000; z++){}
}
void Splash()
{
	ifstream inFile;
	string line;

	if (SHOW_SPLASH) {
		inFile.open(SPLASH_FILE_NAME);
		if (inFile.fail())
			return;
		while (!inFile.eof()) {
			getline(inFile, line);
			chomp(line);
			cout << line << endl;
		}
		Pause();
	}
	inFile.close();
}

void DisplayMainMenu()
{
	cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl;
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(20) << " "
				<< "SUPER WAREHOUSE SALES PROGRAM" << setw(19) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(28) << " " << "MENU OPTIONS"
				<< setw(28) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " " << "1 - PROCESS SALES";
		cout << setw(12) << " " << "2 - MEMBER SERVICES" << setw(10) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " " << "3 - REPORTS MENU";
		cout << setw(13) << " " << "4 - EXIT PROGRAM" << setw(13) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl << endl;
		cout <<setw(5)<< " " << "PLEASE MAKE A MENU SELECTION: ";
}

void DisplayReportsMenu()
{
	cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl;
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(20) << " "
				<< "SUPER WAREHOUSE SALES PROGRAM" << setw(19) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(24) << " " << "REPORTS MENU OPTIONS"
				<< setw(24) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " " << "1 - SALES REPORTS";
		cout << setw(12) << " " << "2 - MEMBER REPORTS" << setw(11) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(23) << " "
				<< "3 - RETURN TO MAIN MENU";
		cout << setw(22) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl << endl;
		cout << setw(5) << " " << "PLEASE MAKE A MENU SELECTION: ";
}

void DisplaySalesMenu()
{
	cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl;
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(20) << " "
				<< "SUPER WAREHOUSE SALES PROGRAM" << setw(19) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(25) << " " << "SALES MENU OPTIONS"
				<< setw(25) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " "
				<< "1 - PROCESS DAILY SALES";
		cout << setw(12) << " " << "2 - REPORTS" << setw(12) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(23) << " "
				<< "3 - RETURN TO MAIN MENU";
		cout << setw(22) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl << endl;
		cout << setw(5) << " " << "PLEASE MAKE A MENU SELECTION: ";
}

void DisplayMembersMenu()
{
	cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl;
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(20) << " "
				<< "SUPER WAREHOUSE SALES PROGRAM" << setw(19) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(24) << " " << "MEMBERS MENU OPTIONS"
				<< setw(24) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " "
				<< "              1 - INITIALIZE MEMBER LIST";
		cout << setw(3) << " " << "" << setw(15) << " "
				<< "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(10) << " " << "2 - REPORTS MENU";
		cout << setw(13) << " " << "3 - RETURN TO MAIN MENU" << setw(6)
				<< " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6) << "*" << setw(68) << " " << "*\n";
		cout << setw(6);
		for(int i = 0; i < 70; i++)
		{
			cout << "*";
		}
		cout << endl << endl;
		cout << setw(5) << " " << "PLEASE MAKE A MENU SELECTION: ";
}

int MenuInput(int maxValue)
{
	int selection;
	while(!(cin >> selection))
	{
			if(!(cin >> selection))
			{
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cout << setw(5) << " "
						<< "Enter Integer(Whole Number) value only\n";
			}
			else if(selection < 1 || selection > maxValue)
			{
				cout << setw(5) << " "  << "Enter a value between 1 and "
						<< maxValue << " only\n";
			}
			cout <<setw(5) << " " << "Please re-enter a selection: ";
	}
		return selection;
}

void PrintSpacer() {
	const int cols = 79;
	const char c = '=';
	int i;
	cout << endl;
	for (i = 0; i < cols; i++) {
		cout << c;
	}
	cout << endl << endl;
}

void Pause() {
	string line;
	cout << "Press RETURN to continue...";
	getline(cin, line);
}
